﻿package plugin.xunbao
{
    import cmd.*;
    import com.core.*;
    import com.sh.game.display.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import consts.*;
    import flash.display.*;
    import flash.events.*;
    import flash.external.*;
    import flash.geom.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import uiresource.xunbao.*;
    import view.*;
    import view.mornExtends.*;
    import view.uilt.*;
    import vo.*;

    public class XunbaoUIExt extends XunbaoUI
    {
        protected var meinvclip:XMLMovieClip;
        protected var meinv:XMLMovieClip;
        private var cbag:CangBag;
        private var cfgitem:Object;
        private var cfgXunbao:Object;
        private var timeNum:int;
        private var vtimeNum:int;
        private var timer:Timer;
        private var viptimer:Timer;
        private var istime:Boolean = false;
        private var vipistime:Boolean = false;
        private var xunbaoMc:XMLMovieClip;
        private var btnArr:Array;
        private var mShowingTip:Boolean = false;
        private var newXuncfg:Object;
        private var tempArr:Array;
        private var tampids:Array;
        private var itemEffect:ItemEffects;
        private var boxitemids:Array;
        private var needArr:Array;
        private var hechen:Array;
        private var showcombox:Array;
        private var _hecgonfig:Object;
        private var clicktime:int = 0;
        private var clickIndex:int = -1;

        public function XunbaoUIExt(param1:XunBao)
        {
            this.tampids = [];
            this.boxitemids = [202119, 202120, 2202118];
            this.needArr = [];
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            this.btnArr = [s1Btn, s2Btn, s3Btn];
            ckview.visible = false;
            this.cfgXunbao = ConfigDictionary.data.worldlevel;
            this.cfgitem = ConfigDictionary.data.item;
            this.newXuncfg = ConfigDictionary.data.fortreasure;
            var _loc_1:* = Control.create(XunbaoControl, this);
            ModelProxy.addObserver("xunbao", _loc_1);
            xbLabel2.text = "1950元宝";
            xbLabel1.text = "9500元宝";
            this.listener();
            this.initshow();
            return;
        }// end function

        public function clearMc() : void
        {
            if (this.xunbaoMc)
            {
                this.xunbaoMc.dispose();
                if (this.xunbaoMc.parent)
                {
                    this.xunbaoMc.parent.removeChild(this.xunbaoMc);
                }
                this.xunbaoMc = null;
            }
            return;
        }// end function

        public function openRefresh(param1:Object) : void
        {
            PluginManager.instance.sendToServer("xunbao", CMDXunBao.GetEggsLog, null);
            this.OpenEggback([]);
            return;
        }// end function

        public function initshow() : void
        {
            var _loc_2:int = 0;
            var _loc_8:Object = null;
            var _loc_9:Object = null;
            var _loc_1:* = ModelProxy.getValue("data", "worldlevel");
            xbLabel.text = "" + ItemUilt.getAllCount(240202011);
            if (_loc_1 > 200)
            {
                _loc_1 = 200;
            }
            var _loc_3:* = this.cfgXunbao[_loc_1];
            if (this.newXuncfg)
            {
                for each (_loc_8 in this.newXuncfg)
                {
                    
                    if (_loc_3.treasure == _loc_8.type)
                    {
                        _loc_2 = _loc_8.show;
                        break;
                    }
                }
            }
            var _loc_4:* = getBoxArr(_loc_2);
            var _loc_5:Array = [];
            var _loc_6:Array = [];
            var _loc_7:int = 1;
            while (_loc_7 <= 13)
            {
                
                _loc_9 = ItemObject.create(this.cfgitem[_loc_4[(_loc_7 - 1)].itemid]);
                if (_loc_7 < 7)
                {
                    _loc_5.push(_loc_9);
                }
                else if (_loc_7 >= 7 && _loc_7 < 13)
                {
                    _loc_6.push(_loc_9);
                }
                else
                {
                    t13.effshow = true;
                    t13.data = _loc_9;
                }
                _loc_7++;
            }
            iconlist.array = _loc_5;
            iconlist1.array = _loc_6;
            iconlist.renderHandler = new Handler(this.reanderHandler);
            iconlist1.renderHandler = new Handler(this.reanderHandler1);
            return;
        }// end function

        private function reanderHandler1(param1:Component, param2:int) : void
        {
            var _loc_3:FlashItem = null;
            if (param2 < iconlist1.array.length)
            {
                _loc_3 = param1.getChildByName("fi") as FlashItem;
                _loc_3.effshow = true;
                _loc_3.data = iconlist1.array[param2];
            }
            return;
        }// end function

        private function reanderHandler(param1:Component, param2:int) : void
        {
            var _loc_3:FlashItem = null;
            if (param2 < iconlist.array.length)
            {
                _loc_3 = param1.getChildByName("fi") as FlashItem;
                _loc_3.effshow = true;
                _loc_3.data = iconlist.array[param2];
            }
            return;
        }// end function

        public function updateckview() : void
        {
            var _loc_4:Object = null;
            if (this.cfgitem == null)
            {
                this.cfgitem = ConfigDictionary.data.item;
            }
            var _loc_1:* = UserData.getBagData();
            if (_loc_1.eggs == null)
            {
                return;
            }
            var _loc_2:* = _loc_1.eggs.source;
            this.tempArr = [];
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                _loc_4 = ItemObject.create(this.cfgitem[_loc_2[_loc_3][0]]);
                if (_loc_4)
                {
                    _loc_4.count = _loc_2[_loc_3][1];
                    this.tempArr.push(_loc_4);
                }
                _loc_3++;
            }
            this.onShow(this.tempArr);
            return;
        }// end function

        public function updatemoney() : void
        {
            var _loc_1:* = UserData.getBagData();
            moneyLabel.text = _loc_1.money + "";
            Label_jf.text = "" + _loc_1.jifen;
            return;
        }// end function

        public function updata() : void
        {
            if (this.visible == false)
            {
                return;
            }
            var _loc_1:* = UserData.getBagData();
            xbLabel.text = "" + ItemUilt.getAllCount(240202011);
            moneyLabel.text = _loc_1.money + "";
            Label_jf.text = "" + _loc_1.jifen;
            this.updateckview();
            this.initshow();
            return;
        }// end function

        private function getChang() : void
        {
            return;
        }// end function

        public function OpenEggback(param1:Array) : void
        {
            var _loc_7:int = 0;
            var _loc_8:Object = null;
            var _loc_9:Object = null;
            var _loc_2:* = ModelProxy.getValue("localCache", "option");
            if (_loc_2 && _loc_2.offbasesound != 1)
            {
                ModelProxy.change("main", "playsound", SoundType.BTN_XUNBAO);
            }
            if (this.cfgitem == null)
            {
                this.cfgitem = ConfigDictionary.data.item;
            }
            var _loc_3:* = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
            var _loc_4:* = new Date();
            new Date().setTime(_loc_3 * 1000);
            var _loc_5:Array = [];
            var _loc_6:int = 0;
            while (_loc_6 < param1.length)
            {
                
                _loc_7 = param1[_loc_6];
                _loc_8 = this.cfgitem[_loc_7];
                if (_loc_8 == null)
                {
                }
                else
                {
                    if (_loc_8.cast != 0)
                    {
                        _list2.addItem("<font color = \'#b9774b\'>通过寻宝获得</font><font color = \'" + MColor.getqualitycolor(_loc_8.quality) + "\'><a href = \'event:" + _loc_8.itemid + "\'><u>" + _loc_8.name + "</u></a></font>");
                    }
                    this.tampids.push("<font color = \'#b9774b\'>通过寻宝获得</font><font color = \'" + MColor.getqualitycolor(_loc_8.quality) + "\'><a href = \'event:" + _loc_8.itemid + "\'><u>" + _loc_8.name + "</u></a></font>");
                    _loc_9 = this.cfgitem[param1[_loc_6]];
                    if (_loc_9.quality > 3)
                    {
                        _loc_5.push(_loc_9);
                    }
                    this.removeArrLe(this.tampids);
                }
                _loc_6++;
            }
            this.itemEffect = new ItemEffects(this);
            this.itemEffect.add(_loc_5);
            _list.array = this.tampids;
            _list.renderHandler = new Handler(this.logRender);
            _list.scrollTo(_list.length - _list.repeatY);
            _list2.scrollTo(_list2.length - _list2.repeatY);
            this.updata();
            return;
        }// end function

        private function logRender(param1:Component, param2:int) : void
        {
            var _loc_3:Label = null;
            if (param2 < _list.array.length)
            {
                _loc_3 = param1.getChildByName("logtxt") as Label;
                _loc_3.text = _list.array[param2];
                _loc_3.addEventListener(TextEvent.LINK, this.linkHander);
            }
            return;
        }// end function

        public function GetEggsLog(param1:Object) : void
        {
            var _loc_3:String = null;
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_2:Array = [];
            if (this.cfgitem == null)
            {
                this.cfgitem = ConfigDictionary.data.item;
            }
            for (_loc_3 in param1)
            {
                
                _loc_4 = param1[_loc_3];
                _loc_5 = this.cfgitem[_loc_4[2]];
                if (_loc_5 == null)
                {
                    continue;
                }
                _loc_2.push("<font color=\'#53b436\'>[" + _loc_4[1] + "]</font><font color=\'#997f60\'>通过寻宝获得</font><font color = \'" + MColor.getqualitycolor(_loc_5.quality) + "\'><a href=\'event:" + _loc_5.itemid + "\'><u>" + _loc_5.name + "</u></a></font>");
                this.removeArrLe(_loc_2);
            }
            _list2.array = _loc_2;
            _list2.renderHandler = new Handler(this.logRender2);
            _list2.scrollTo(_list2.length - _list.repeatY);
            return;
        }// end function

        private function removeArrLe(param1:Array) : void
        {
            if (param1.length > 30)
            {
                param1.shift();
            }
            return;
        }// end function

        private function logRender2(param1:Component, param2:int) : void
        {
            var _loc_3:Label = null;
            if (param2 < _list2.array.length)
            {
                _loc_3 = param1.getChildByName("logtxt") as Label;
                _loc_3.text = _list2.array[param2];
                _loc_3.addEventListener(TextEvent.LINK, this.linkHander);
            }
            return;
        }// end function

        protected function linkHander(event:TextEvent) : void
        {
            var itemInfo:Object;
            var event:* = event;
            event.stopImmediatePropagation();
            var links:* = int(event.text);
            var p:* = new Point(GlobalLayer.instance.root.mouseX, GlobalLayer.instance.root.mouseY);
            if (links)
            {
                if (this.mShowingTip)
                {
                    this.mShowingTip = false;
                    GlobalLayer.instance.root.removeEventListener(MouseEvent.CLICK, this.checkCloseTipHandler);
                    ModelProxy.change("tips", "state", "close");
                }
                itemInfo = ConfigDictionary.data.item[links];
                if (itemInfo.itemtype == 1)
                {
                    ModelProxy.change("tips", "arm", {data:itemInfo, p:p});
                }
                else
                {
                    ModelProxy.change("tips", "tool", {data:itemInfo, p:p});
                }
                this.mShowingTip = true;
            }
            var fun:* = function () : void
            {
                GlobalLayer.instance.root.addEventListener(MouseEvent.CLICK, checkCloseTipHandler);
                return;
            }// end function
            ;
            setTimeout(fun, 100);
            return;
        }// end function

        private function checkCloseTipHandler(event:MouseEvent) : void
        {
            if (this.mShowingTip)
            {
                this.mShowingTip = false;
                GlobalLayer.instance.root.removeEventListener(MouseEvent.CLICK, this.checkCloseTipHandler);
                ModelProxy.change("tips", "state", "close");
            }
            return;
        }// end function

        public function xbcangku(param1:Point) : void
        {
            if (ckview)
            {
            }
            return;
        }// end function

        private function listener() : void
        {
            closeBtn.addEventListener(MouseEvent.CLICK, this.closeHandler);
            var _loc_1:int = 0;
            while (_loc_1 < this.btnArr.length)
            {
                
                this.btnArr[_loc_1].addEventListener(MouseEvent.MOUSE_OVER, this.showtipsFunc);
                this.btnArr[_loc_1].addEventListener(MouseEvent.MOUSE_OUT, this.closetipsFunc);
                _loc_1++;
            }
            s1Btn.addEventListener(MouseEvent.CLICK, this.soshenFunc1);
            s2Btn.addEventListener(MouseEvent.CLICK, this.soshenFunc2);
            s3Btn.addEventListener(MouseEvent.CLICK, this.soshenFunc3);
            openCkBtn.addEventListener(MouseEvent.CLICK, this.xbckFunc);
            btn_payLink.clickHandler = new Handler(this.payHandler);
            duihuanLink.addEventListener(MouseEvent.CLICK, this.closeHandler1);
            ckview.closebtn.addEventListener(MouseEvent.CLICK, this.xbckFunc);
            ckview.Btn_yijian.clickHandler = new Handler(this.alltiquHandler);
            goumaoLink.addEventListener(MouseEvent.CLICK, this.goingBuy);
            ckview.list.renderHandler = new Handler(this.itemRender);
            ckview.list.mouseHandler = new Handler(this.clickitem);
            return;
        }// end function

        protected function goingBuy(event:MouseEvent) : void
        {
            DialogMgr.instance.open(AppDialog.SC_BUY, {storeId:225});
            return;
        }// end function

        protected function closeHandler1(event:MouseEvent) : void
        {
            PluginManager.instance.openPlugin("xunbaoshop", duihuanLink.localToGlobal(new Point(0, 0)), 2);
            return;
        }// end function

        protected function closetipsFunc(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "state", "close");
            return;
        }// end function

        protected function showtipsFunc(event:MouseEvent) : void
        {
            var _loc_2:String = "";
            switch(event.currentTarget)
            {
                case s1Btn:
                {
                    _loc_2 = "消耗200元宝或1个寻宝符，可获得1积分，优先消耗寻宝符!";
                    break;
                }
                case s2Btn:
                {
                    _loc_2 = "消耗1950元宝或10个寻宝符，可获得10积分，优先消耗寻宝符！";
                    break;
                }
                case s3Btn:
                {
                    _loc_2 = "消耗9500元宝或50个寻宝符，可获得50积分，优先消耗寻宝符!";
                    break;
                }
                default:
                {
                    break;
                }
            }
            ModelProxy.change("tips", "normal", {data:_loc_2, p:new Point(mouseX, mouseY + 5)});
            return;
        }// end function

        private function payHandler() : void
        {
            if (!ItemUilt.canpay)
            {
                ModelProxy.change("movetips", "error", "功能尚未开放，敬请期待");
                return;
            }
            ExternalInterface.call("topay");
            return;
        }// end function

        protected function xbckFunc(event:MouseEvent) : void
        {
            ckview.visible = !ckview.visible;
            this.updateckview();
            return;
        }// end function

        protected function closeHandler(event:MouseEvent) : void
        {
            PluginManager.instance.closePlugin("xunbao");
            this.clearMc();
            return;
        }// end function

        protected function palyMC(param1:int) : void
        {
            this.xunbaoMc = XMLMcManager.instance.getPluginMc("xunbao", "xunbao");
            if (this.xunbaoMc)
            {
                this.xunbaoMc.blendMode = BlendMode.ADD;
                this.xunbaoMc.mouseChildren = false;
                this.xunbaoMc.mouseEnabled = false;
                this.xunbaoMc.x = 179;
                this.xunbaoMc.y = 168;
                this.addChild(this.xunbaoMc);
                if (this.xunbaoMc.totalFrame)
                {
                    this.xunbaoMc.playFromTo(-1, (this.xunbaoMc.totalFrame - 1), new Handler(this.stopMC, [param1]));
                }
                else
                {
                    this.stopMC(param1);
                }
            }
            else
            {
                this.stopMC(param1);
            }
            return;
        }// end function

        private function stopMC(param1:int) : void
        {
            if (this.xunbaoMc)
            {
                this.removeChild(this.xunbaoMc);
            }
            var _loc_2:* = new Object();
            _loc_2.count = param1;
            if (param1 > 1)
            {
                ckview.visible = true;
            }
            var _loc_3:int = 0;
            while (_loc_3 < this.btnArr.length)
            {
                
                this.btnArr[_loc_3].disabled = false;
                _loc_3++;
            }
            PluginManager.instance.sendToServer("xunbao", CMDXunBao.OpenEgg, _loc_2);
            return;
        }// end function

        protected function soshenFunc1(event:MouseEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = UserData.getBagData("money");
            if (_loc_2 >= 200 || ItemUilt.getAllCount(240202011) > 0)
            {
                _loc_3 = 0;
                while (_loc_3 < this.btnArr.length)
                {
                    
                    this.btnArr[_loc_3].disabled = true;
                    _loc_3++;
                }
                this.palyMC(1);
            }
            else
            {
                ModelProxy.change("movetips", "error", "您的元宝或道具不足，请充值！");
            }
            return;
        }// end function

        protected function soshenFunc2(event:MouseEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = UserData.getBagData("money");
            if (_loc_2 >= 1950 || ItemUilt.getAllCount(240202011) >= 10)
            {
                _loc_3 = 0;
                while (_loc_3 < this.btnArr.length)
                {
                    
                    this.btnArr[_loc_3].disabled = true;
                    _loc_3++;
                }
                this.palyMC(10);
                ckview.visible = true;
            }
            else
            {
                ModelProxy.change("movetips", "error", "您的元宝或道具不足，请充值！");
            }
            return;
        }// end function

        protected function soshenFunc3(event:MouseEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = UserData.getBagData("money");
            if (_loc_2 >= 9500 || ItemUilt.getAllCount(240202011) >= 50)
            {
                _loc_3 = 0;
                while (_loc_3 < this.btnArr.length)
                {
                    
                    this.btnArr[_loc_3].disabled = true;
                    _loc_3++;
                }
                ckview.visible = true;
                this.palyMC(50);
            }
            else
            {
                ModelProxy.change("movetips", "error", "您的元宝或道具不足，请充值！");
            }
            return;
        }// end function

        public function onShow(param1:Array) : void
        {
            ckview.page.text = param1.length + "/300";
            ckview.list.array = new Array(300);
            return;
        }// end function

        private function itemRender(param1:Component, param2:int) : void
        {
            var _loc_3:Image = null;
            var _loc_4:Object = null;
            var _loc_5:FlashItem = null;
            if (param2 < ckview.list.length)
            {
                _loc_3 = param1.getChildByName("itembg") as Image;
                _loc_4 = this.tempArr[param2];
                _loc_5 = param1.getChildByName("fitem") as FlashItem;
                if (_loc_4)
                {
                    _loc_5.effshow = true;
                    _loc_5.data = _loc_4;
                    _loc_5.doubleClickEnabled = true;
                }
                else
                {
                    _loc_5.data = null;
                    _loc_5.doubleClickEnabled = false;
                }
            }
            return;
        }// end function

        private function clickitem(event:MouseEvent, param2:int) : void
        {
            switch(event.type)
            {
                case MouseEvent.CLICK:
                {
                    if (getTimer() - this.clicktime < 300)
                    {
                        this.clicktime = getTimer();
                        this.doubleClickHandler(param2);
                        return;
                    }
                    this.clicktime = getTimer();
                    this.clickIndex = param2;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function alltiquHandler() : void
        {
            var _loc_1:* = ckview.Btn_yijian.localToGlobal(new Point(0, 0));
            ModelProxy.change("data", "itempoint", _loc_1);
            PluginManager.instance.sendToServer("xunbao", CMDXunBao.getEggItemAll, null);
            return;
        }// end function

        protected function doubleClickHandler(param1:int) : void
        {
            PluginManager.instance.sendToServer("xunbao", CMDXunBao.getEggItem, param1);
            return;
        }// end function

        protected function duihuanFunc(param1:int) : void
        {
            PluginManager.instance.sendToServer("xunbao", CMDXunBao.BayThings, param1);
            return;
        }// end function

        public static function getBoxArr(param1:int) : Array
        {
            var _loc_9:String = null;
            var _loc_10:Array = null;
            var _loc_11:Object = null;
            var _loc_2:Array = [];
            var _loc_3:* = ConfigDictionary.data.box;
            var _loc_4:* = ConfigDictionary.data.item;
            var _loc_5:* = _loc_3[param1];
            var _loc_6:* = (_loc_3[param1].items as String).split("#");
            var _loc_7:int = -1;
            var _loc_8:int = 0;
            while (_loc_8 < _loc_6.length)
            {
                
                _loc_9 = _loc_6[_loc_8];
                _loc_10 = _loc_9.split("-");
                if (_loc_10.length >= 5 && _loc_10[4] > 0 && _loc_10[4] != UserData.getUserData("career"))
                {
                }
                else if (_loc_10.length >= 4 && _loc_10[3] != 2 && _loc_10[3] != UserData.getUserData("sex"))
                {
                }
                else
                {
                    _loc_11 = ItemObject.create(_loc_4[_loc_10[0]]);
                    if (_loc_11)
                    {
                        _loc_11.count = _loc_10[1];
                        if (_loc_10[5])
                        {
                            _loc_11.timeount = _loc_10[5];
                        }
                        _loc_2.push(_loc_11);
                    }
                }
                _loc_8++;
            }
            return _loc_2;
        }// end function

    }
}
